<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Promotion;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Promotion\Ctl\StatDeliverCtl;
use Srv\Apps\Proto\DTO\Promotion\StatDeliverAddDTO;
use Srv\Apps\Proto\DTO\Promotion\StatDeliverEditDTO;
use Srv\Apps\Proto\DTO\Promotion\StatDeliverListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Promotion\StatDeliverListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class StatDeliverAct extends ActionAbstract
{
    /**
     * @url /stat/deliver/list
     * @auth statDeliverList
     * @req Srv.Apps.Proto.DTO.Promotion.StatDeliverListDTO
     * @rsp Srv.Apps.Proto.VO.Promotion.StatDeliverListVO
     * 每日投放列表
     */
    public function getStatDeliverList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof StatDeliverListDTO) || !($VOClass instanceof StatDeliverListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = StatDeliverCtl::tag()->getStatDeliverList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /stat/deliver/add
     * @auth statDeliverAdd
     * @req Srv.Apps.Proto.DTO.Promotion.StatDeliverAddDTO
     * 每日投放新增
     */
    public function statDeliverAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof StatDeliverAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $logData        = $setData;
        $sderId         = StatDeliverCtl::tag()->statDeliverAdd($setData);
        if($sderId < 1){
            $errMsg     = '每日投放新增失败';
            $LogCtl->logAdd(LogCtl::STAT_DELIVER_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '每日投放新增成功';
        $LogCtl->logAdd(LogCtl::STAT_DELIVER_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /stat/deliver/edit
     * @auth statDeliverEdit
     * @req Srv.Apps.Proto.DTO.Promotion.StatDeliverEditDTO
     * 每日投放编辑
     */
    public function statDeliverEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof StatDeliverEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $sderId         = intval($DTOClass->getSderId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $StatDeliverCtl = StatDeliverCtl::tag();
        $infoSrc        = $StatDeliverCtl->getStatDeliverSrc($sderId);
        if(!isset($infoSrc['sder_id'])) {
            $logData    = $setData;
            $errMsg     = '每日投放不存在';
            $LogCtl->logAdd(LogCtl::STAT_DELIVER_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $StatDeliverCtl->statDeliverEdit($sderId, $setData);
        $logData        = ['new' => $setData, 'old' => $infoSrc];
        if(!$result){
            $errMsg     = '每日投放不存在';
            $LogCtl->logAdd(LogCtl::STAT_DELIVER_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '每日投放编辑成功';
        $LogCtl->logAdd(LogCtl::STAT_DELIVER_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}